package cn.sdormitory.job.dormitory;

import cn.hutool.core.collection.CollectionUtil;
import cn.sdormitory.attendance.dao.HolidayConfigDao;
import cn.sdormitory.attendance.entity.HolidayConfig;
import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.dto.StudentDept;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.smartdor.dao.SdAttenceDao;
import cn.sdormitory.smartdor.entity.SdAttence;
import cn.sdormitory.smartdor.service.SdAttenceService;
import cn.sdormitory.sys.dao.SysConfigDao;
import cn.sdormitory.sys.dao.SysDeptDao;
import cn.sdormitory.sys.entity.SysConfig;
import cn.sdormitory.sys.entity.SysDept;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.collections4.ListUtils;
import org.apache.http.client.utils.DateUtils;
import org.mockito.internal.util.collections.ListUtil;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: lx
 * @date: 2023/9/14 11:09
 * @description: 宿舍缺勤数据新增Job：自动记录每天未归寝的学生，为其新增未归寝的打卡记录
 */
public class DormitoryAbsenceDataInsertJob extends QuartzJobBean {

    @Autowired
    private SdAttenceDao sdAttenceDao;

    @Autowired
    private BStudentDao bStudentDao;

    @Autowired
    private HolidayConfigDao holidayConfigDao;

    @Autowired
    private SysDeptDao sysDeptDao;
    
    @Autowired
    private SysConfigDao sysConfigDao;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Date date = new Date();
        String accessDate = DateUtils.formatDate(date, "yyyy-MM-dd");
//        String accessDate = "2024-09-20";
        List<SdAttence> allTodayRecords = sdAttenceDao.getAllTodayRecords(accessDate);

//        List<HolidayConfig> holiday = holidayConfigDao.getHolidayByDate(accessDate);

        //提交数据列表
        List<SdAttence> sdAttendanceList = new ArrayList<>();

        //查询所有住宿学生
        LambdaQueryWrapper<BStudent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BStudent::getStatus,"1");
        List<BStudent> allStudent = bStudentDao.selectList(wrapper);

        for(BStudent student : allStudent){
//            //是否放假
//            boolean flag = false;
//            //如果该学生所在部门放假，则不进行数据插入
//            StudentDept studentDept = bStudentDao.getStudentDept(student.getStudentNo());
//            if(holiday != null && holiday.size() > 0){
//                for(HolidayConfig item : holiday){
//                    //判断当前放假部门是否有子部门
//                    List<SysDept> deptChildId = sysDeptDao.getChildDepts(item.getDeptId());
//                    List<Long> childIds = new ArrayList<>();
//                    if (CollectionUtil.isNotEmpty(deptChildId)) {
//                        for (SysDept sysDept : deptChildId) {
//                        childIds.add(sysDept.getId());
//                        }
//                    }
//                    flag = childIds.stream().anyMatch(temp -> temp.equals(studentDept.getDeptId()));
////                    Long match = deptChildId.stream().filter(temp -> temp.equals(studentDept.getDeptId()))
////                            .findFirst().get();
//                    if(flag){
//                        break;
//                    }
//                }
//            }
//            //放假
//            if(flag){
//                continue;
//            }

            //考勤机上是否存在打卡记录
            boolean isClockIn = false;
            for(SdAttence sdAttence : allTodayRecords){
                //判断当天学生当天是否有宿舍考勤打卡记录
                if(student.getStudentNo().equals(sdAttence.getStudentNo())){
                    isClockIn = true;
                }
            }
            if(!isClockIn){
                SdAttence insert = new SdAttence();
                insert.setStudentNo(student.getStudentNo());
                try {
                    insert.setAccessDate(new SimpleDateFormat("yyyy-MM-dd").parse(accessDate));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                insert.setAttenceStatus("2");
                insert.setBdormitoryId(student.getBdormitoryId());
                insert.setBuildingNo(student.getBuildingNo());
                insert.setStorey(student.getStorey());
                insert.setDormitoryNo(student.getDormitoryNo());
                insert.setFlag("4");
                //当日内不允许重复打卡
//                List<SdAttence> attenceList = sdAttenceDao.getAttenceByDateAndStudentNo(accessDate,insert.getStudentNo());

//                if(attenceList == null || attenceList.size() < 1) {
                    sdAttendanceList.add(insert);
//                }
            }
        }
        List<List<SdAttence>> partition = ListUtils.partition(sdAttendanceList, 100);
        for (List<SdAttence> item : partition) {
            sdAttenceDao.insertBatch(item);
        }
    }

}
